import { Controller, Get, Post, Body, Patch, Param, Delete, UseInterceptors, UploadedFile, HttpStatus, Req, Res, Query } from '@nestjs/common';
import { UserAccountService } from './user_account.service';
import { CreateUserAccountDto } from './dto/create-user_account.dto';
import { UpdateUserAccountDto } from './dto/update-user_account.dto';
import { UpdateUserInfo } from './dto/edit-userInfo.dto'
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import * as svgCaptcha from 'svg-captcha';

// import { UserAcountPipe } from './user_acount.pipe';

import { FileInterceptor } from '@nestjs/platform-express';

@ApiTags('用户账户')
@Controller('user-account')
export class UserAccountController {
  constructor(private readonly userAccountService: UserAccountService) { }

  @Post()
  @ApiOperation({ summary: '创建账户' })
  @ApiParam({ name: "createUserAccountDto", description: "账户信息", required: true })
  create(@Body() createUserAccountDto: CreateUserAccountDto) {
    return this.userAccountService.create(createUserAccountDto);
  }

  @Get()
  findAll() {
    return this.userAccountService.findAll();
  }

  //查找单个用户（登录）
  @Post('login')
  @ApiOperation({ summary: '登录' })
  login(@Req() req, @Body() accountDto: { account: string, password: string, code: string }) {
    //先判断验证码
    if (req.session.code && req.session.code.toLocaleLowerCase() === accountDto.code.toLocaleLowerCase()) {
      return this.userAccountService.login(accountDto);
    } else {
      return {
        code: HttpStatus.BAD_REQUEST,
        msg: '验证码错误'
      }
    }
  }
  //通过uuid查找用户
  @Post('byUUID')
  @ApiOperation({ summary: '查找用户（uuid）' })
  getByUUID(@Body() uuidDto: { uuid: string }) {
    return this.userAccountService.findOneByUUID(uuidDto);
  }


  @Patch()
  @ApiParam({ name: "updateUserAccountDto", description: "账户信息", required: true })
  update(@Body() updateUserAccountDto: UpdateUserAccountDto) {
    return this.userAccountService.update(updateUserAccountDto);
  }

  @Delete(':uuid')
  remove(@Param('uuid') uuid: string) {
    return this.userAccountService.remove(uuid);
  }


  //关注用户
  @Post('follow')
  followUp(@Body() followDto: { uuid: string, follow_uuid: string }) {
    return this.userAccountService.followUp(followDto)
  }


  //通过UUID更新用户信息
  @Post('updateUserInfo')
  updateUserInfo(@Body() updateUserInfoDto: UpdateUserInfo) {
    return this.userAccountService.updateUserInfo(updateUserInfoDto)
  }


  //上传头像
  @Post('album')
  @UseInterceptors(FileInterceptor('file'))
  upload(@UploadedFile() file) {
    // console.log(file);

    const data = {
      imgUrl: `http://127.0.0.1:3000/xiaoluo/images/${file.filename}`,
      fileName: file.filename
    }
    return {
      code: HttpStatus.OK,
      msg: '上传成功',
      data
    }
  }

  //收藏文章
  @Post('collectionArticle')
  articleCollect(@Body() articleCollectDto: { uuid: string, articleId: number }) {
    return this.userAccountService.articleCollect(articleCollectDto)
  }

  //查询收藏的文章
  @Post('collectedArticle')
  findAllCollectedArticle(@Body() collectedArticleDto: { uuid: string, keyword?: string }) {
    return this.userAccountService.findAllCollectedArticle(collectedArticleDto)
  }

  //点赞文章
  @Post('supportArticle')
  supportArticle(@Body() supportArticleDto: { uuid: string, articleId: number }) {
    return this.userAccountService.supportArticle(supportArticleDto)
  }

  //删除浏览记录
  @Post('delHistory')
  delHistory(@Body() delHistoryDto: { uuid: string, articleId: number }) {
    return this.userAccountService.delHistory(delHistoryDto)
  }


  //创建验证码
  @Get('code')
  createCaptcha(@Req() req, @Res() res) {
    //验证码
    const captcha = svgCaptcha.create({
      size: 4, //字符的个数
      fontSize: Math.floor(Math.random() * 10) + 40, //生成40~50大小的验证码
      width: 120, //宽度
      height: 32, //高度
      ignoreChars: '0oO1ilI',
      noise: 2, // 干扰线
      color: true,
      background: 'white', 
    });
    //存储验证码记录到session
    req.session.code = captcha.text;
    //设置返回值类型，并返回
    res.type('image/svg+xml');
    res.send(captcha.data);
  }

  //发布公告
  @Post('announcement')
  publicAnnouncement(@Body() announcementDto: { id?: number, content: string, title: string }) {
    return this.userAccountService.publicAnnouncement(announcementDto)
  }
  //查看公告
  @Get('getAnnouncement')
  getAnnouncement() {
    return this.userAccountService.getAnnouncement()
  }

}
